import { pair, SSAF, ITEMS, SAD, ATTS } from "../../config/config";
import bus from "../../eventBus/eventBus";
import formula, { calculateEval, formulaSum, handleResult, shiftLogicCode, systemEnter, toTheValue } from "../../formula/index";
import * as calculate from "@/util/xc/calculate";
export const countMixins = {
  data() {
    return {
      ...calculate
    };
  },
  methods: {

    /**
     * @param {String} type 需要计算的值
     * @param {Object} data row
     */
    switchCount(type, data) {
      const n = 0;
      switch (type) {
        // 社保公积金表
        case SSAF.SSAF_PERSONAL_ANNUITY[n]: // 个人养老
        case SSAF.SSAF_PERSONAL_UNEMPLOYMENT[n]: // 个人失业
        case SSAF.SSAF_PERSONAL_MEDICAL[n]: // 个人医疗
        case SSAF.SSAF_PERSONAL_INJURY[n]: // 个人工伤
        case SSAF.SSAF_PERSONAL_BEAR[n]: // 个人生育
          this.switchCount(ITEMS.PERSONAL_SOCIAL_SECURITY[n], data);
          break;
        case SSAF.SSAF_PERSONAL_RESERVE_FUND[n]: // 个人公积金
          this.switchCount(ITEMS.PERSONAL_RESERVE_FUND[n], data);
          this.switchCount(SSAF.SSAF_PERSONAL_SOCIAL_FUND_TOTAL[n], data);
          break;

        case SSAF.SSAF_PERSONAL_SOCIAL_FUND_TOTAL[n]: // 个人社保公积金合计
          data[type] = formula.oneSelfSum(data[SSAF.SSAF_SOCIAL_SECURITY_TOTAL[n]], data[SSAF.SSAF_PERSONAL_RESERVE_FUND[n]]);

          this.switchCount(ITEMS.ACCUMULATED_TAXABLE_INCOME[n], data);
          this.switchCount(ITEMS.ACTUAL_SALARY[n], data);
          this.switchCount(ITEMS.SALARY_TAXABLE[n], data);
          break;

        case SSAF.SSAF_COMPANY_ANNUITY[n]: // 企业养老
        case SSAF.SSAF_COMPANY_UNEMPLOYMENT[n]: // 企业失业
        case SSAF.SSAF_COMPANY_MEDICAL[n]: // 企业医疗
        case SSAF.SSAF_COMPANY_INJURY[n]: // 企业工伤
        case SSAF.SSAF_COMPANY_BEAR[n]: // 企业生育
        case SSAF.SSAF_COMPANY_RESERVE_FUND[n]: // 企业公积金
          this.switchCount(SSAF.SSAF_COMPANY_SOCIAL_FUND_TOTAL[n], data);
          break;

        case SSAF.SSAF_COMPANY_SOCIAL_FUND_TOTAL[n]: // 企业社保公积金合计
          const gssb = formulaSum([SSAF.SSAF_COMPANY_ANNUITY[n], SSAF.SSAF_COMPANY_UNEMPLOYMENT[n], SSAF.SSAF_COMPANY_MEDICAL[n], SSAF.SSAF_COMPANY_INJURY[n], SSAF.SSAF_COMPANY_BEAR[n]].map(key => data[key]));
          data[type] = formula.companySum(gssb, data[SSAF.SSAF_COMPANY_RESERVE_FUND[n]]);
          break;

        // 专项附加扣除表
        case SAD.SAD_ACCUMULATED_EDUCATION[n]: // 累计子女教育
        case SAD.SAD_ACCUMULATED_HOUSE[n]: // 累计住房租金
        case SAD.SAD_ACCUMULATED_LOANS[n]: // 累计住房贷款
        case SAD.SAD_ACCUMULATED_CONTINUING[n]: // 累计继续教育
        case SAD.SAD_ACCUMULATED_ELDER[n]: // 累计赡养老人
        case SAD.SAD_ACCUMULATED_INFANT[n]: // 累计婴幼儿陪护
          this.switchCount(SAD.SAD_ACCUMULATED_SPECIAL_DEDUCTION_TOTAL[n], data);
          break;

        case SAD.SAD_ACCUMULATED_SPECIAL_DEDUCTION_TOTAL[n]: // 累计专项附加扣除合计
          data[type] = formula.accrueDeduct(...[SAD.SAD_ACCUMULATED_EDUCATION[n], SAD.SAD_ACCUMULATED_HOUSE[n], SAD.SAD_ACCUMULATED_LOANS[n], SAD.SAD_ACCUMULATED_CONTINUING[n], SAD.SAD_ACCUMULATED_ELDER[n], SAD.SAD_ACCUMULATED_INFANT[n]].map(key => data[key]));
          this.switchCount(ITEMS.ACCUMULATED_SPECIAL_DEDUCTION[n], data);
          break;

        // 工资主表
        case ITEMS.PERSONAL_SOCIAL_SECURITY[n]: // 个人社保
          this.$set(data, type, formulaSum([SSAF.SSAF_PERSONAL_ANNUITY[n], SSAF.SSAF_PERSONAL_UNEMPLOYMENT[n], SSAF.SSAF_PERSONAL_MEDICAL[n], SSAF.SSAF_PERSONAL_INJURY[n], SSAF.SSAF_PERSONAL_BEAR[n]].map(key => data[key])));
          this.switchCount(SSAF.SSAF_PERSONAL_SOCIAL_FUND_TOTAL[n], data);
          break;
        case ITEMS.PERSONAL_RESERVE_FUND[n]: // 个人公积金
          this.$set(data, type, data[SSAF.SSAF_PERSONAL_RESERVE_FUND[n]]);
          break;
        case ITEMS.ACCUMULATED_INCOME_AS_OF[n]: // 累计收入额 （截至上月）
          this.switchCount(ITEMS.ACCUMULATED_INCOME[n], data);
          break;
        case ITEMS.ACCUMULATED_SPECIAL_DEDUCTION_AS_OF[n]: // "累计专项扣除 （截至上月）
          this.switchCount(ITEMS.ACCUMULATED_TAXABLE_INCOME[n], data);
          break;
        case ITEMS.ACCUMULATED_ADVANCE_TAX_AS_OF[n]: // 累计已预缴税额 （截至上月）
          this.switchCount(ITEMS.ADVANCE_TAX[n], data);
          break;
        case ITEMS.ACCUMULATED_INCOME[n]: // 累计收入额
          data[type] = formula.accrueIncome(data[ITEMS.ACCUMULATED_INCOME_AS_OF[n]], data[ITEMS.SHOULD_SALARY[n]]);
          this.switchCount(ITEMS.ACCUMULATED_TAXABLE_INCOME[n], data);
          break;
        case ITEMS.ACCUMULATED_DEDUCTION_COST[n]: // 累计减除费用
          data[type] = formula.accrueReduction(data);
          this.switchCount(ITEMS.ACCUMULATED_TAXABLE_INCOME[n], data);
          this.switchCount(ITEMS.SALARY_TAXABLE[n], data);
          break;
        case ITEMS.ACCUMULATED_SPECIAL_DEDUCTION[n]: // 累计专项附加扣除
          this.$set(data, type, data[SAD.SAD_ACCUMULATED_SPECIAL_DEDUCTION_TOTAL[n]]);
          this.switchCount(ITEMS.ACCUMULATED_TAXABLE_INCOME[n], data);
          this.switchCount(ITEMS.SALARY_TAXABLE[n], data);
          break;
        case ITEMS.ACCUMULATED_TAXABLE_INCOME[n]: // 累计应纳税所得额
          data[type] = formula.accrueTaxableIncome(data);
          this.switchCount(ITEMS.ACCUMULATED_TAXABLE[n], data);
          break;
        case ITEMS.ACCUMULATED_TAXABLE[n]: // 累计应纳税额
          data[type] = formula.accrueTaxable(data);
          this.switchCount(ITEMS.PERSONAL_INCOME_TAX[n], data);
          break;
        case ITEMS.ADVANCE_TAX[n]: // 已预缴税额
          data[type] = formula.advanceTax(data[ITEMS.ACCUMULATED_ADVANCE_TAX_AS_OF[n]]);
          this.switchCount(ITEMS.PERSONAL_INCOME_TAX[n], data);
          break;
        case ITEMS.PERSONAL_INCOME_TAX[n]: // 个人所得税
          data[type] = formula.oneselfTax(data[ITEMS.ACCUMULATED_TAXABLE[n]], data[ITEMS.ADVANCE_TAX[[n]]]);
          this.switchCount(ITEMS.ACTUAL_SALARY[n], data);
          break;
        case ITEMS.SHOULD_SALARY[n]: // 应发工资
          this.switchCount(ITEMS.ACCUMULATED_INCOME[n], data);
          break;

        // 考勤表
        case ATTS.ATTS_LATE_DEDUCTIONS[n]: // 迟到扣款
        case ATTS.ATTS_EARLY_DEDUCTIONS[n]: // 早退扣款
        case ATTS.ATTS_LACK_DEDUCTIONS[n]: // 缺卡扣款
        case ATTS.ATTS_NO_DUTY[n]: // 不在职免发
        case ATTS.ATTS_ABSENTEEISM_DEDUCTIONS[n]: // 旷工扣款
        case ATTS.ATTS_LEAVE_DEDUCTIONS[n]: // 请假扣款
          this.switchCount(ITEMS.ATTENDANCE_DEDUCTION_TOTAL[n], data);
          break;
        case ITEMS.ATTENDANCE_DEDUCTION_TOTAL[n]:
          data[type] = formula.attsDeductionTotal(data[ATTS.ATTS_LATE_DEDUCTIONS[n]], data[ATTS.ATTS_EARLY_DEDUCTIONS[n]], data[ATTS.ATTS_LACK_DEDUCTIONS[n]], data[ATTS.ATTS_NO_DUTY[n]], data[ATTS.ATTS_ABSENTEEISM_DEDUCTIONS[n]], data[ATTS.ATTS_LEAVE_DEDUCTIONS[n]], ...bus.attendanceObj[data.userId].map(key => data[key]));
          break;
        case ATTS.ATTS_WORKDAY_OVERTIME_PAY[n]: // 工作日加班费
        case ATTS.ATTS_WEEKEND_OVERTIME_PAY[n]: // 周末加班费
        case ATTS.ATTS_FESTIVALS_OVERTIME_PAY[n]: // 节假日加班费
          this.switchCount(ITEMS.OVERTIME_PAY_TOTAL[n], data);
          break;
        case ATTS.ATTS_OVERTIME_PAY_TOTAL[n]: // 加班费合计
        case ITEMS.OVERTIME_PAY_TOTAL[n]: // 加班费合计-主表
          data[ATTS.ATTS_OVERTIME_PAY_TOTAL[n]] = data[ITEMS.OVERTIME_PAY_TOTAL[n]] = formula.overtimePay(data[ATTS.ATTS_WORKDAY_OVERTIME_PAY[n]], data[ATTS.ATTS_WEEKEND_OVERTIME_PAY[n]], data[ATTS.ATTS_FESTIVALS_OVERTIME_PAY[n]]);
          break;

        default:
          if (bus.attendanceObj[data.userId]?.includes(type)) {
            this.switchCount(ITEMS.ATTENDANCE_DEDUCTION_TOTAL[n], data);
          }
          break;
      }

      console.groupCollapsed("开始计算公式", type, data);
      this.switchFormula(type, data);
      console.groupEnd("开始计算公式");
    },
    // 自定义公式相关
    ...{

      /** 自定义公式 */
      switchFormula(type, data) {
        const attr = pair.getDimMatch(type);
        const arr = this.isHit(attr[2]);
        arr.forEach(item => {
          this.countFormula(item, data);
        });
      },

      /** 计算自定义公式 */
      countFormula(obj, data) {
        const n = 0;
        const key = pair.getNameMatch(obj.name)[n] || obj.namePy;
        console.groupCollapsed("单个公式计算结果并赋值", key, obj, data);
        data[key] = obj.salaryRule == 10 ? shiftLogicCode(obj.namePy, data) : 
          obj.salaryRule == 11 ? systemEnter(obj.namePy, data) : 
            handleResult(calculateEval(toTheValue(obj.formulaDetail, data)));

        if (key == ITEMS.SALARY_TAXABLE[n]) {
          const val = data[key] - data[ITEMS.ACCUMULATED_DEDUCTION_COST[n]];
          console.log("应税工资", data[key], val);
          data[key] = handleResult(val < 0 ? 0 : val);
        }
        this.switchCount(key, data);
        console.groupEnd();
      },

      /** 命中的自定义 */
      isHit(type) {
        console.group("获取命中公式", type, bus.formulaArr);
        if (!type) return [];
        const [namePy, id] = type.split("_");
        const arr = bus.formulaArr.filter(item => item.formulaDetail?.includes(namePy));
        console.log("命中公式", arr, type);
        console.groupEnd();
        return arr;
      }
    }
  }
};
